package com.asa.voice.mp3.asa.instream;

import com.asa.voice.mp3.asa.decoder.IAudio;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;

public class BuffRandReadURL extends RandomRead {
	  private static final int BLOCKLEN = 32768;
	  
	  private static final int BUFLEN = 524288;
	  
	  private static final int OFFMASK = 524287;
	  
	  private static final int BUFFERSIZE = 491520;
	  
	  private int offset;
	  
	  private byte[] lock;
	  
	  private byte[] buf;
	  
	  private int bufsize;
	  
	  private boolean acceptRanges;
	  
	  private volatile boolean eof;
	  
	  private IAudio audio;
	  
	  private HttpConnection connection;
	  
	  public BuffRandReadURL(IAudio paramIAudio) {
	    this.audio = paramIAudio;
	    this.buf = new byte[524288];
	    this.lock = new byte[0];
	    this.connection = new HttpConnection();
	  }
	  
	  public boolean open(String paramString1, String paramString2) throws IOException {
	    String str1 = paramString1.substring(paramString1.lastIndexOf("/") + 1);
	    String str2 = URLDecoder.decode(str1, "GBK");
	    str1 = URLDecoder.decode(str1, "UTF-8");
	    if (str1.length() > str2.length())
	      str1 = str2; 
	    if (this.audio != null && paramString2 != null)
	      this.audio.refreshMessage(paramString2); 
	    this.connection.open(new URL(paramString1), null);
	    int i = this.connection.getResponseCode();
	    if (i < 200 || i >= 300)
	      return printErrMsg("URL Connection Fails. ResponseCode: " + i + ", ResponseMessage: " + this.connection.getResponseMessage()); 
	    if ((this.length = this.connection.getContentLength()) <= 0L)
	      return printErrMsg("Failed to get file length."); 
	    System.out.println("\nPLAY>> " + str1);
	    this.acceptRanges = "bytes".equals(this.connection.getHeaderField("Accept-Ranges"));
	    Writer writer = new Writer();
	    writer.setName("writer_thrad");
	    writer.setPriority(7);
	    writer.start();
	    return true;
	  }
	  
	  private boolean printErrMsg(String paramString) {
	    System.out.println();
	    this.connection.printResponse();
	    System.err.println(paramString);
	    return false;
	  }
	  
	  public int read(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) {
	    synchronized (this.lock) {
	      while (this.bufsize < paramInt2 && !this.eof) {
	        try {
	          waitForBuffering();
	        } catch (InterruptedException interruptedException) {
	          return -1;
	        } 
	      } 
	      if (this.bufsize == 0)
	        return -1; 
	      if (this.bufsize < paramInt2)
	        paramInt2 = this.bufsize; 
	    } 
	    int i = this.offset & 0x7FFFF;
	    int j = 524288 - i;
	    if (j < paramInt2) {
	      System.arraycopy(this.buf, i, paramArrayOfbyte, paramInt1, j);
	      System.arraycopy(this.buf, 0, paramArrayOfbyte, paramInt1 + j, paramInt2 - j);
	    } else {
	      System.arraycopy(this.buf, i, paramArrayOfbyte, paramInt1, paramInt2);
	    } 
	    synchronized (this.lock) {
	      this.bufsize -= paramInt2;
	    } 
	    this.offset += paramInt2;
	    synchronized (this.buf) {
	      this.buf.notify();
	    } 
	    return paramInt2;
	  }
	  
	  private void waitForBuffering() throws InterruptedException {
	    long l = System.currentTimeMillis();
	    while (this.bufsize < 491520 && !this.eof) {
	      this.lock.wait();
	      long l1;
	      if ((l1 = System.currentTimeMillis() - l) < 200L)
	        continue; 
	      float f = 512000.0F / (float)l1;
	      String str = String.format("\rbuffered: %6.2f%%, %6.02fKB/s ", new Object[] { Float.valueOf(100.0F * this.bufsize / 524288.0F), Float.valueOf(f) });
	      if (this.audio != null) {
	        this.audio.refreshMessage(str);
	      } else {
	        System.out.print(str);
	      } 
	      if (l1 > 10000L && f < 8.0F) {
	        System.out.println("\nDownloading speed too slow,please try again later.");
	        close();
	        break;
	      } 
	    } 
	    System.out.print("\n");
	  }
	  
	  public long getFilePointer() {
	    return this.offset;
	  }
	  
	  public void close() {
	    this.eof = true;
	    synchronized (this.buf) {
	      this.buf.notify();
	    } 
	    try {
	      this.connection.close();
	    } catch (IOException iOException) {}
	  }
	  
	  public boolean seek(long paramLong) {
	    if (!this.acceptRanges)
	      return false; 
	    return false;
	  }
	  
	  private class Writer extends Thread {
	    private Writer() {}
	    
	    public void run() {
	      // Byte code:
	      //   0: iconst_0
	      //   1: istore_2
	      //   2: iconst_0
	      //   3: istore_3
	      //   4: iconst_0
	      //   5: istore #4
	      //   7: lconst_0
	      //   8: lstore #5
	      //   10: aload_0
	      //   11: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   14: invokestatic access$100 : (Ljmp123/instream/BuffRandReadURL;)Ljmp123/instream/HttpConnection;
	      //   17: invokevirtual getInputStream : ()Ljava/io/InputStream;
	      //   20: astore #7
	      //   22: aload #7
	      //   24: ifnonnull -> 28
	      //   27: return
	      //   28: aload_0
	      //   29: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   32: invokestatic access$200 : (Ljmp123/instream/BuffRandReadURL;)Z
	      //   35: ifne -> 372
	      //   38: iload #4
	      //   40: ifne -> 142
	      //   43: aload_0
	      //   44: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   47: invokestatic access$200 : (Ljmp123/instream/BuffRandReadURL;)Z
	      //   50: ifne -> 134
	      //   53: aload_0
	      //   54: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   57: invokestatic access$300 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   60: dup
	      //   61: astore #8
	      //   63: monitorenter
	      //   64: aload_0
	      //   65: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   68: invokestatic access$400 : (Ljmp123/instream/BuffRandReadURL;)I
	      //   71: ldc 491520
	      //   73: if_icmpgt -> 82
	      //   76: aload #8
	      //   78: monitorexit
	      //   79: goto -> 134
	      //   82: aload #8
	      //   84: monitorexit
	      //   85: goto -> 96
	      //   88: astore #9
	      //   90: aload #8
	      //   92: monitorexit
	      //   93: aload #9
	      //   95: athrow
	      //   96: aload_0
	      //   97: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   100: invokestatic access$500 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   103: dup
	      //   104: astore #8
	      //   106: monitorenter
	      //   107: aload_0
	      //   108: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   111: invokestatic access$500 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   114: invokevirtual wait : ()V
	      //   117: aload #8
	      //   119: monitorexit
	      //   120: goto -> 131
	      //   123: astore #10
	      //   125: aload #8
	      //   127: monitorexit
	      //   128: aload #10
	      //   130: athrow
	      //   131: goto -> 43
	      //   134: iload_2
	      //   135: ldc 524287
	      //   137: iand
	      //   138: istore_2
	      //   139: ldc 32768
	      //   141: istore_3
	      //   142: iload_3
	      //   143: ifle -> 242
	      //   146: aload_0
	      //   147: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   150: invokestatic access$200 : (Ljmp123/instream/BuffRandReadURL;)Z
	      //   153: ifne -> 242
	      //   156: iload_3
	      //   157: sipush #4096
	      //   160: if_icmpge -> 167
	      //   163: iload_3
	      //   164: goto -> 170
	      //   167: sipush #4096
	      //   170: istore_1
	      //   171: aload #7
	      //   173: aload_0
	      //   174: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   177: invokestatic access$500 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   180: iload_2
	      //   181: iload_1
	      //   182: invokevirtual read : ([BII)I
	      //   185: dup
	      //   186: istore_1
	      //   187: iconst_m1
	      //   188: if_icmpne -> 203
	      //   191: aload_0
	      //   192: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   195: iconst_1
	      //   196: invokestatic access$202 : (Ljmp123/instream/BuffRandReadURL;Z)Z
	      //   199: pop
	      //   200: goto -> 242
	      //   203: iload_3
	      //   204: iload_1
	      //   205: isub
	      //   206: istore_3
	      //   207: iload_2
	      //   208: iload_1
	      //   209: iadd
	      //   210: istore_2
	      //   211: lload #5
	      //   213: iload_1
	      //   214: i2l
	      //   215: ladd
	      //   216: dup2
	      //   217: lstore #5
	      //   219: aload_0
	      //   220: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   223: getfield length : J
	      //   226: lcmp
	      //   227: iflt -> 142
	      //   230: aload_0
	      //   231: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   234: iconst_1
	      //   235: invokestatic access$202 : (Ljmp123/instream/BuffRandReadURL;Z)Z
	      //   238: pop
	      //   239: goto -> 242
	      //   242: goto -> 319
	      //   245: astore #8
	      //   247: iinc #4, 1
	      //   250: getstatic java/lang/System.out : Ljava/io/PrintStream;
	      //   253: ldc '[B# %,d] Timeout, retry=%d\\n'
	      //   255: iconst_2
	      //   256: anewarray java/lang/Object
	      //   259: dup
	      //   260: iconst_0
	      //   261: lload #5
	      //   263: invokestatic valueOf : (J)Ljava/lang/Long;
	      //   266: aastore
	      //   267: dup
	      //   268: iconst_1
	      //   269: iload #4
	      //   271: invokestatic valueOf : (I)Ljava/lang/Integer;
	      //   274: aastore
	      //   275: invokevirtual printf : (Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;
	      //   278: pop
	      //   279: iload #4
	      //   281: bipush #10
	      //   283: if_icmpge -> 289
	      //   286: goto -> 28
	      //   289: getstatic java/lang/System.out : Ljava/io/PrintStream;
	      //   292: ldc 'B# %,d: out of retries. Giving up.\\n'
	      //   294: iconst_1
	      //   295: anewarray java/lang/Object
	      //   298: dup
	      //   299: iconst_0
	      //   300: lload #5
	      //   302: invokestatic valueOf : (J)Ljava/lang/Long;
	      //   305: aastore
	      //   306: invokevirtual printf : (Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;
	      //   309: pop
	      //   310: aload_0
	      //   311: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   314: iconst_1
	      //   315: invokestatic access$202 : (Ljmp123/instream/BuffRandReadURL;Z)Z
	      //   318: pop
	      //   319: iconst_0
	      //   320: istore #4
	      //   322: aload_0
	      //   323: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   326: invokestatic access$300 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   329: dup
	      //   330: astore #8
	      //   332: monitorenter
	      //   333: aload_0
	      //   334: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   337: ldc 32768
	      //   339: iload_3
	      //   340: isub
	      //   341: invokestatic access$412 : (Ljmp123/instream/BuffRandReadURL;I)I
	      //   344: pop
	      //   345: aload_0
	      //   346: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   349: invokestatic access$300 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   352: invokevirtual notify : ()V
	      //   355: aload #8
	      //   357: monitorexit
	      //   358: goto -> 369
	      //   361: astore #11
	      //   363: aload #8
	      //   365: monitorexit
	      //   366: aload #11
	      //   368: athrow
	      //   369: goto -> 28
	      //   372: aload_0
	      //   373: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   376: iconst_1
	      //   377: invokestatic access$202 : (Ljmp123/instream/BuffRandReadURL;Z)Z
	      //   380: pop
	      //   381: aload_0
	      //   382: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   385: invokestatic access$300 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   388: dup
	      //   389: astore #8
	      //   391: monitorenter
	      //   392: aload_0
	      //   393: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   396: invokestatic access$300 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   399: invokevirtual notify : ()V
	      //   402: aload #8
	      //   404: monitorexit
	      //   405: goto -> 416
	      //   408: astore #12
	      //   410: aload #8
	      //   412: monitorexit
	      //   413: aload #12
	      //   415: athrow
	      //   416: aload #7
	      //   418: invokevirtual close : ()V
	      //   421: goto -> 576
	      //   424: astore #8
	      //   426: goto -> 576
	      //   429: astore #8
	      //   431: getstatic java/lang/System.out : Ljava/io/PrintStream;
	      //   434: new java/lang/StringBuilder
	      //   437: dup
	      //   438: invokespecial <init> : ()V
	      //   441: ldc 'BuffRandReadURL.Writer.run(): '
	      //   443: invokevirtual append : (Ljava/lang/String;)Ljava/lang/StringBuilder;
	      //   446: aload #8
	      //   448: invokevirtual toString : ()Ljava/lang/String;
	      //   451: invokevirtual append : (Ljava/lang/String;)Ljava/lang/StringBuilder;
	      //   454: invokevirtual toString : ()Ljava/lang/String;
	      //   457: invokevirtual println : (Ljava/lang/String;)V
	      //   460: aload_0
	      //   461: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   464: iconst_1
	      //   465: invokestatic access$202 : (Ljmp123/instream/BuffRandReadURL;Z)Z
	      //   468: pop
	      //   469: aload_0
	      //   470: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   473: invokestatic access$300 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   476: dup
	      //   477: astore #8
	      //   479: monitorenter
	      //   480: aload_0
	      //   481: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   484: invokestatic access$300 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   487: invokevirtual notify : ()V
	      //   490: aload #8
	      //   492: monitorexit
	      //   493: goto -> 504
	      //   496: astore #13
	      //   498: aload #8
	      //   500: monitorexit
	      //   501: aload #13
	      //   503: athrow
	      //   504: aload #7
	      //   506: invokevirtual close : ()V
	      //   509: goto -> 576
	      //   512: astore #8
	      //   514: goto -> 576
	      //   517: astore #14
	      //   519: aload_0
	      //   520: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   523: iconst_1
	      //   524: invokestatic access$202 : (Ljmp123/instream/BuffRandReadURL;Z)Z
	      //   527: pop
	      //   528: aload_0
	      //   529: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   532: invokestatic access$300 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   535: dup
	      //   536: astore #15
	      //   538: monitorenter
	      //   539: aload_0
	      //   540: getfield this$0 : Ljmp123/instream/BuffRandReadURL;
	      //   543: invokestatic access$300 : (Ljmp123/instream/BuffRandReadURL;)[B
	      //   546: invokevirtual notify : ()V
	      //   549: aload #15
	      //   551: monitorexit
	      //   552: goto -> 563
	      //   555: astore #16
	      //   557: aload #15
	      //   559: monitorexit
	      //   560: aload #16
	      //   562: athrow
	      //   563: aload #7
	      //   565: invokevirtual close : ()V
	      //   568: goto -> 573
	      //   571: astore #15
	      //   573: aload #14
	      //   575: athrow
	      //   576: return
	      // Line number table:
	      //   Java source line number -> byte code offset
	      //   #194	-> 0
	      //   #195	-> 7
	      //   #196	-> 10
	      //   #197	-> 22
	      //   #198	-> 27
	      //   #201	-> 28
	      //   #203	-> 38
	      //   #204	-> 43
	      //   #205	-> 53
	      //   #206	-> 64
	      //   #207	-> 76
	      //   #208	-> 82
	      //   #209	-> 96
	      //   #210	-> 107
	      //   #211	-> 117
	      //   #213	-> 134
	      //   #214	-> 139
	      //   #219	-> 142
	      //   #220	-> 156
	      //   #221	-> 171
	      //   #222	-> 191
	      //   #223	-> 200
	      //   #225	-> 203
	      //   #226	-> 207
	      //   #227	-> 211
	      //   #228	-> 230
	      //   #229	-> 239
	      //   #240	-> 242
	      //   #233	-> 245
	      //   #234	-> 247
	      //   #235	-> 250
	      //   #236	-> 279
	      //   #237	-> 286
	      //   #238	-> 289
	      //   #239	-> 310
	      //   #241	-> 319
	      //   #244	-> 322
	      //   #245	-> 333
	      //   #246	-> 345
	      //   #247	-> 355
	      //   #252	-> 372
	      //   #253	-> 381
	      //   #254	-> 392
	      //   #255	-> 402
	      //   #257	-> 416
	      //   #259	-> 421
	      //   #258	-> 424
	      //   #260	-> 426
	      //   #249	-> 429
	      //   #250	-> 431
	      //   #252	-> 460
	      //   #253	-> 469
	      //   #254	-> 480
	      //   #255	-> 490
	      //   #257	-> 504
	      //   #259	-> 509
	      //   #258	-> 512
	      //   #260	-> 514
	      //   #252	-> 517
	      //   #253	-> 528
	      //   #254	-> 539
	      //   #255	-> 549
	      //   #257	-> 563
	      //   #259	-> 568
	      //   #258	-> 571
	      //   #259	-> 573
	      //   #262	-> 576
	      // Exception table:
	      //   from	to	target	type
	      //   28	372	429	java/lang/Exception
	      //   28	372	517	finally
	      //   64	79	88	finally
	      //   82	85	88	finally
	      //   88	93	88	finally
	      //   107	120	123	finally
	      //   123	128	123	finally
	      //   142	242	245	java/net/SocketTimeoutException
	      //   333	358	361	finally
	      //   361	366	361	finally
	      //   392	405	408	finally
	      //   408	413	408	finally
	      //   416	421	424	java/io/IOException
	      //   429	460	517	finally
	      //   480	493	496	finally
	      //   496	501	496	finally
	      //   504	509	512	java/io/IOException
	      //   517	519	517	finally
	      //   539	552	555	finally
	      //   555	560	555	finally
	      //   563	568	571	java/io/IOException
	    }
	  }
}

